<? 
	if(isset($_POST['btngo']))
	{
		require("required/dbconnect.php");
		$pageno=$_POST['pageno'];
		$main_cat=$_POST['main_cat'];
		$sub_cat=$_POST['sub_cat'];
		$keywordtype=$_POST['keyword_type'];
		$keyword=mysql_escape_string($_POST['keyword']);
		$price_from=$_POST['price_from'];
		$price_to=$_POST['price_to'];
		$status=$_POST['status'];
		header("Location:product_search.php?page=$pageno&main_cat=$main_cat&sub_cat=$sub_cat&keyword=$keyword&keyword_type=$keywordtype&price_from=$price_from&price_to=$price_to&status=$status");
	}

	require("required/dbconnect.php"); 	
	require("required/function.php"); 
	require("required/header.php"); 
	
	
	checkAccess("allow_product_management",1); 
	
	if(empty($_GET['newsearch']))
	{
		if(empty($_GET['page']))
		{
			$page="1";
		}
		else
		{
			$page=$_GET['page'];
		}
		
		$totalperpage=9;
	
		$from=($page-1)*$totalperpage;
		
		if(isset($_POST['btnsearch']))
		{
			$main_cat=$_POST['main_cat'];
			$main_cat_slt="";
			$sub_cat=$_POST['sub_cat'];
			$subcatslt="";
			$keywordtype=$_POST['keyword_type'];
			$keyword=mysql_escape_string($_POST['keyword']);
			$price_from=$_POST['price_from'];
			$price_to=$_POST['price_to'];
			$status=$_POST['status'];
			
			if($main_cat=="")
			{
				$main_cat_q="";
			}
			else
			{
				$main_cat_q="and cat_id='$main_cat' ";
			}
			
			if($sub_cat=="" or $sub_cat=="-1")
			{
				$sub_cat_q="";
			}
			else
			{
				$sub_cat_q="and sub_cat_id='$sub_cat' ";
			}
			
			if($price_from=="" and $price_to!="")
			{
				$price_q="and price<$price_to";
			}
			elseif($price_to=="" and $price_from!="")
			{
				$price_q="and price>$price_to";
			}
			elseif($price_to=="" and $price_from=="")
			{
				$price_q="";
			}
			elseif($price_to!="" and $price_from!="")
			{
				$price_q="and (price between $price_from and $price_to)";
			}
				
			if($status!="")
			{
				if($status==1)
				{
					$status_q="and status=1";
					$status_slt1="selected";
					$status_slt2="";
				}
				else
				{
					$status_q="and status=0";
					$status_slt1="";
					$status_slt2="selected";
				}
								
			}
			else
			{
				$status_q="";
				$status_slt1="";
				$status_slt2="";
				
			}
						
			if($keyword!="")
			{
				if($keywordtype==1)
				{
					$keyword_q="and (product_name_eng like '%$keyword%')";
					$kt0="";
					$kt1="selected";
					$kt2="";
					$kt3="";
				}
				elseif($keywordtype==2)
				{
					$keyword_q="and (description_eng like '%$keyword%' or description_eng like '%$keyword%')";
					$kt0="";
					$kt1="";
					$kt2="selected";
					$kt3="";
				}
				elseif($keywordtype==3)
				{
					$keyword_q="and (product_code like '%$keyword%')";
					$kt0="";
					$kt1="";
					$kt2="";
					$kt3="selected";
				}
				else
				{
					$keyword_q="and (product_name_eng like '%$keyword%' or product_name_eng like '%$keyword%' or description_eng like '%$keyword%' or description_eng like '%$keyword%')";
					$kt0="selected";
					$kt1="";
					$kt2="";
					$kt3="";
				}
			}
			else
			{
				$keyword_q="";
				$kt0="";
				$kt1="";
				$kt2="";
				$kt3="";
			}
			
			
			$get_product=mysql_query("select * from os_products where status=1 $main_cat_q $sub_cat_q $keyword_q $price_q $status_q order by cat_id asc, sub_cat_id asc limit $from,$totalperpage")or die(mysql_error());
			$get_total=mysql_query("select * from os_products where status=1 $main_cat_q $sub_cat_q $keyword_q $price_q $status_q")or die(mysql_error());
			
			//echo "select * from os_products where status=1 $main_cat_q $sub_cat_q $keyword_q $price_q order by cat_id asc, sub_cat_id asc limit $from,$totalperpage";
		
		}
		else
		{
			if(empty($_GET['main_cat']))
			{
				$main_cat="";
				$main_cat_slt="selected";
				$main_cat_q="";
			}
			else
			{
				$main_cat=$_GET['main_cat'];
				$main_cat_slt="";
				$main_cat_q="and cat_id='$main_cat' ";
			}
			
			if(empty($_GET['sub_cat']))
			{
				$sub_cat="";
				$sub_cat_slt="selected";
				$sub_cat_q="";
			}
			else
			{
				$sub_cat=$_GET['sub_cat'];
				$sub_cat_slt="";
				if($sub_cat!='-1')
				{
					$sub_cat_q="and sub_cat_id='$sub_cat' ";
				}
				else
				{
					$sub_cat_q="";
				}
			}
			
			if(empty($_GET['price_from']))
			{
				$price_from="";
			}
			else
			{
				$price_from=$_GET['price_from'];
			}
			
			if(empty($_GET['price_to']))
			{
				$price_to="";
			}
			else
			{
				$price_to=$_GET['price_to'];
			}
			
			if($price_from=="" and $price_to!="")
			{
				$price_q="and price<$price_to";
			}
			elseif($price_to=="" and $price_from!="")
			{
				$price_q="and price>$price_to";
			}
			elseif($price_to=="" and $price_from=="")
			{
				$price_q="";
			}
			elseif($price_to!="" and $price_from!="")
			{
				$price_q="and (price between $price_from and $price_to)";
			}
			
			if($status!="")
			{
				if($status==1)
				{
					$status_q="and status=1";
					$status_slt1="selected";
					$status_slt2="";
				}
				else
				{
					$status_q="and status=0";
					$status_slt1="";
					$status_slt2="selected";
				}
								
			}
			else
			{
				$status_q="";
				$status_slt1="";
				$status_slt2="";
				
			}
					
			if(empty($_GET['keyword']))
			{
				$keyword="";
				$keyword_q="";
				$kt0="";
				$kt1="";
				$kt2="";
				$kt3="";
				$keywordtype="";
			}
			else
			{
				$keyword=$_GET['keyword'];
				$keywordtype=$_GET['keywordtype'];
				if($keywordtype==1)
				{
					$keyword_q="and (product_name_eng like '%$keyword%')";
					$kt0="";
					$kt1="selected";
					$kt2="";
					$kt3="";
				}
				elseif($keywordtype==2)
				{
					$keyword_q="and (description_eng like '%$keyword%' or description_eng like '%$keyword%')";
					$kt0="";
					$kt1="";
					$kt2="selected";
					$kt3="";
				}
				elseif($keywordtype==3)
				{
					$keyword_q="and (product_code like '%$keyword%')";
					$kt0="";
					$kt1="";
					$kt2="";
					$kt3="selected";
				}
				else
				{
					$keyword_q="and (product_name_eng like '%$keyword%' or product_name_eng like '%$keyword%' or description_eng like '%$keyword%' or description_eng like '%$keyword%')";
					$kt0="selected";
					$kt1="";
					$kt2="";
					$kt3="";
				}
				
			}
			
			$get_product=mysql_query("select * from os_products where status=1 $main_cat_q $sub_cat_q $keyword_q $price_q $status order by cat_id asc, sub_cat_id asc limit $from,$totalperpage")or die(mysql_error());
			$get_total=mysql_query("select * from os_products where status=1 $main_cat_q $sub_cat_q $keyword_q $price_q $status")or die(mysql_error());
		}
		
		$count=mysql_num_rows($get_total);	
	
		$totalpage=$count/$totalperpage;
		
		if($count%$totalperpage>0)
		{
			if(round($totalpage)>$totalpage)
			{
				$totalpage=round($totalpage);
			}
			else
			{
				$totalpage=round($totalpage+1);
			}
		}
		else
		{
			$totalpage=round($totalpage);
		}
		
		
		$nextpage=$page+1;
		$prepage=$page-1;
		$tr1="";
		$tr2="";
	
	?>
		<table width="100%"  cellpadding="0" cellspacing="5" height="300">
			
			<td width="100%" align="left" valign="top">
				<table width="100%">
					<tr>
						<Td align="center">
							<form action="product_search.php" method="post" name="frm">
							<table width="100%" cellpadding="3" cellspacing="0" class="solidborder">
								<tr class="table_header">
									<td colspan="5"><strong>Product Search</strong></td>
								</tr>
								<script language="JavaScript" type="text/javascript" src="required/js/goAjax.js"></script>
                                <script language="JavaScript" type="text/javascript" src="../required/js/select_cat.js"></script>
								<?
									$get_cat=mysql_query("select * from os_category order by id asc");
								
								?>
								<tr>
									<Td width="20%">Category</td><td width="24%">
										<select name="main_cat" class="text" onChange="showSubCat(this.value)">
											<option value="" <?= $main_cat_slt ?>>All</option>
										<?
											while($c=mysql_fetch_array($get_cat,MYSQL_ASSOC))
											{
												if($c['id']==$main_cat)
												{
													$cslt="selected";
												}
												else
												{
													$cslt="";
												}
										?>
											<option value="<?= $c['id'] ?>" <?= $cslt ?>><?= $c['name_eng'] ?></option>
										<? } ?>
										</select>
										
									</td>
									<td width="24%">Sub category</td><td width="32%">
						
										<div id="divResult">
										<?
											if($main_cat!="")
											{
												$get_subcat=mysql_query("select * from os_sub_category where main_category=".$main_cat." order by id desc");
												if($sub_cat=='-1')
												{
													$soption="<option value='-1' selected>All</option>";
													$soption2="";
												}
												else
												{
													$soption="<option value=''>Please select</option>";
													$soption2="<option value='-1'>All</option>";
												}
										?>
												<select name="sub_cat" class="text">
												<?= $soption ?>
												<? 
													while($s=mysql_fetch_array($get_subcat,MYSQL_ASSOC))
													{
														if($s['id']==$sub_cat)
														{
															$sslt="selected";
														}
														else
														{
															$sslt="";
														}	
														?>
														<option <?= $sslt ?> value="<?= $s['id'] ?>"><?= $s['name_eng'] ?> </option>
														<? 
													
													}
												?>
												<?= $soption2 ?>
												</select>
										<?	
											}else{
										?>				
										
										<select name="sub_cat" class="text">
											<option value="">All</option>
										</select>		
										<? } ?>
										</div>
									</td>
								</tr>
								<tr>
									<td width="20%">Keyword</td><td><input type="text" name="keyword" size="20" maxlength="50" class="text" value="<?= $keyword ?>"></td>
									<td>Keyword type</td><td>
										<select name="keyword_type" class="text">
											<option value=""  <?= $kt0 ?>>All type</option>
											<option value="1" <?= $kt1 ?>>Product name</option>
											<option value="2" <?= $kt2 ?>>Product description</option>
											<option value="3" <?= $kt3 ?>>Product Code</option>
										</select>
									</td>
								</tr>
								<tr>
									<td>Price range</td><td><input type="text" class="text" size="2" name="price_from" value="<?= $price_from ?>"> - <input type="text" class="text" size="2" name="price_to" value="<?= $price_to ?>"></td>
                                    <td>Status</td><td>
                                        <select name="status" class="text">
                                            <option value="">All Status</option>
                                            <option value="1" <?= $status_slt1 ?> >Active</option>
                                            <option value="0" <?= $status_slt2 ?>>Inactive</option>							
                                        </select>
                                    </td>
								</tr>
								<tr>
									<td align="center"><input type="submit" value="Submit" class="button" name="btnsearch"></td>
								</tr>
				 			 </table>
				 			 </form>
				 		 </Td>
					</tr>
					<tr>
					<Td>
						<table width="100%">
							<?	
								if($count==0)
								{
							?>
								<tr>
									<td align="center"> :: NO RECORD :: </td>
								</tr>
							<?	
								}
								else
								{
							?>
								<tr>
									<td width="" colspan="6" align="left">
										<table width="100%" cellpadding="0" cellspacing="0" >
											<td width="69%" height="45">
											<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
											<input type="button" class="button" value="<<" onClick="window.open('product_search.php?page=1&main_cat=<?= $main_cat ?>&sub_cat=<?= $sub_cat ?>&keyword=<?= $keyword ?>&keywordtype=<?= $keywordtype ?>&price_from=<?= $price_from ?>&price_to=<?= $price_to ?>','_self')">
											<input type="button" class="button" value="<" onClick="window.open('product_search.php?page=<?= $prepage ?>&main_cat=<?= $main_cat ?>&sub_cat=<?= $sub_cat ?>&keyword=<?= $keyword ?>&keywordtype=<?= $keywordtype ?>&price_from=<?= $price_from ?>&price_to=<?= $price_to ?>','_self')">
											
											Page <input type="text" name="pageno" value="<?= $page ?>" size="2" maxlength="3" class="text"> of <?= $totalpage ?>
											
											<input type="hidden" name="main_cat" value="<?= $main_cat ?>">
											<input type="hidden" name="sub_cat" value="<?= $sub_cat ?>">
											<input type="hidden" name="keyword" value="<?= $keyword ?>">
											<input type="hidden" name="keyword_type" value="<?= $keywordtype ?>">
											<input type="hidden" name="price_from" value="<?= $price_from ?>">
											<input type="hidden" name="price_to" value="<?= $price_to ?>">
											<input type="submit" value="Go" class="button" name="btngo">
											<? if($page==$totalpage){ ?>	
											<input type="button" class="button" value=">" >
											<? }else{ ?>
											<input type="button" class="button" value=">" onClick="window.open('product_search.php?page=<?= $nextpage ?>&main_cat=<?= $main_cat ?>&sub_cat=<?= $sub_cat ?>&keyword=<?= $keyword ?>&keywordtype=<?= $keywordtype ?>&price_from=<?= $price_from ?>&price_to=<?= $price_to ?>','_self')">
											<? } ?>
											<input type="button" class="button" value=">>" onClick="window.open('product_search.php?page=<?= $totalpage ?>&main_cat=<?= $main_cat ?>&sub_cat=<?= $sub_cat ?>&keyword=<?= $keyword ?>&keywordtype=<?= $keywordtype ?>&price_from=<?= $price_from ?>&price_to=<?= $price_to ?>','_self')">
											
											</form>
											
											</td>
											
										</table>
									</td>
								</tr>
								<?
									$no=0;
									productAvatarAdmin($get_product); 
								?>
								<?
									}
								?>
						</table>
					</Td>
				</tr>		  
			</table>			
		</td>
		</table>	
<?
	}
	else
	{
?>
		<table width="100%"  cellpadding="0" cellspacing="5" height="300">
		
			<td width="100%" align="left" valign="top">
			<table width="100%">
				
				<tr>
					<Td align="center"><form action="product_search.php" method="post" name="frm">
				<table width="100%" cellpadding="3" cellspacing="0" class="solidborder">
				<tr class="table_header">
				<td colspan="5">Product search</td>
				</tr>
				<script language="JavaScript" type="text/javascript" src="required/js/goAjax.js"></script>
                 <script language="JavaScript" type="text/javascript" src="required/js/select_cat.js"></script>
				<?
					$get_cat=mysql_query("select * from os_category order by id asc");
				
				?>
				<tr>
					<Td width="20%">Category</td><td width="24%">
						<select name="main_cat" class="text" onChange="showSubCat(this.value)">
							<option value="" >All</option>
						<?
							while($c=mysql_fetch_array($get_cat,MYSQL_ASSOC))
							{
								
						?>
							<option value="<?= $c['id'] ?>"><?= $c['name_eng'] ?></option>
						<? } ?>
						</select>
						
					</td>
					<td width="24%">Sub Category</td><td width="32%">
					
					<div id="divResult">
					
					
					<select name="sub_cat" class="text">
						<option value="">All</option>
					</select>		
					
				</div>
					</td>
				</tr>
				<tr>
					<td width="20%">Keyword</td><td><input type="text" name="keyword" size="20" maxlength="50" class="text" value=""></td>
					<td>Keyword type</td><td>
						<select name="keyword_type" class="text">
							<option value=""  >All type</option>
							<option value="1" >Product name</option>
							<option value="2" >Product description</option>
							<option value="3" >Product code</option>
						</select>
					</td>
				</tr>
				<tr>
					<td>Price range</td><td><input type="text" class="text" size="2" name="price_from" value=""> - <input type="text" class="text" size="2" name="price_to" value=""></td>
                    <td>Status</td><td>
						<select name="status" class="text">
                        	<option value="">All Status</option>
							<option value="1">Active</option>
							<option value="0">Inactive</option>							
						</select>
					</td>
				</tr>
				<tr>
					<td align="center"><input type="submit" value="Submit" class="button" name="btnsearch"></td>
				</tr>
			  </table>
			  </form></Td>
				</tr>
				
			</table>	
		</td>
		</table>
				
		
	
<? }require("required/footer.php") ?>		